package tcards.android;

import models.Status;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class UnitField extends LinearLayout {
	private TextView mLabel;
	private LinearLayout mContent;
	
	private void init(AttributeSet attrs) {
		TypedArray tArray = getContext().obtainStyledAttributes(attrs, R.styleable.Field);
		mLabel =  (TextView) findViewById(R.id.unitLabel);
		mContent =  (LinearLayout) findViewById(R.id.content);
		
		String text = tArray.getString(R.styleable.Field_label);
        if (text != null) { 
        	mLabel.setText(text);
        }
        
		int unitColor = tArray.getColor(R.styleable.Field_unitColor, Color.WHITE);
		mLabel.setTextColor(unitColor);
		
		tArray.recycle();
	}	
	
	public UnitField(Context context) {
		this(context, null, 0);
	}

	public UnitField(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public UnitField(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		LayoutInflater li = LayoutInflater.from(context);
		addView(li.inflate(R.layout.unit_field, this, false));
		if (attrs != null) {
			init(attrs);
		}
		setOnDragListener(new View.OnDragListener() {			
			public boolean onDrag(View v, DragEvent event) {				
				switch (event.getAction()) {
				case DragEvent.ACTION_DROP:
					TcardsAndroidActivity activity = (TcardsAndroidActivity) v.getContext();
					UnitPage page = activity.getCurrentPage();
					UnitPage correspondingPage;
					Status newStatus = Status.toStatus(((UnitField) v).getLabel().getText().toString());
					LinearLayout content = page.findField(newStatus).getContent();
					TcardButton button = (TcardButton) event.getLocalState();
					TcardButton correspondingButton = activity.getCorrespondingTcardButton(button);
					if (activity.getCurrentKind() == null) {
						correspondingPage = activity.getPage(button.getTcard().getKind());
					}
					else {
						correspondingPage = activity.getPage(null);
					}
					
					button.getTcard().setStatus(newStatus);
					
					activity.updateTcard(button.getTcard());
					
					((ViewGroup) button.getParent()).removeView(button);
					content.addView(button);
					
					//Move addButton to the bottom of the field
					View addButton = content.findViewById(R.id.addButton);
					content.removeView(addButton);
					content.addView(addButton);
					
					content = correspondingPage.findField(newStatus).getContent();
					
					((ViewGroup) correspondingButton.getParent()).removeView(correspondingButton);
					content.addView(correspondingButton);
					
					//Move addButton to the bottom of the field
					addButton = content.findViewById(R.id.addButton);
					content.removeView(addButton);
					content.addView(addButton);
					break;
				}
				return true;
			}
		});
	}

	public TextView getLabel() {
		return mLabel;
	}
	
	public LinearLayout getContent() {
		return mContent;
	}
}
